#include "BreakThrough.h"


BreakThrough::BreakThrough(void) : Game() {
	m_gameName = "BreakThrough";
}

BreakThrough::BreakThrough(int columns, int rows) : Game(columns,rows) {
	m_gameName = "BreakThrough";
}

BreakThrough::~BreakThrough(void)
{
}

bool BreakThrough::legalMove( const Move &gameMove ){
	// if one forward, then check forward field is empty
	// if 
	bool legal;
	int verticalMax;
	std::string error;
	if(Player1 == currentPlayer()) verticalMax = 1;
	else verticalMax = -1;
	int occupancy = gameBoard().getOccupancy(gameMove.t_col,gameMove.t_row);
	// Checks vertical difference must be = abs(1)
	if( verticalMax == (gameMove.t_row-gameMove.f_row)) {
		int horizontalDelta = std::abs(gameMove.f_col-gameMove.t_col);
		// Checks horizontal difference, must be abs(1) or 0
		if(1 == horizontalDelta ) {
			// Checks if target diagonal field is occupied by thou.
			if(currentPlayer() == occupancy) {
				legal = false;
			} else {
				legal = true;
			}
		} 
		// If Move straight, field must be empty
		else if (0 == horizontalDelta )  {
			// If spot infront is occupied fail plz
			if ( None != occupancy ) {
				legal = false;
			}
			else {
				legal = true;
			}
		} 
		// Moving to far, or somthing in the way.
		else {
			legal = false;
		}
	} 
	else {
		legal = false;
	}
	return legal;
}

bool BreakThrough::winCondition(){
	// if black on 1 or white on n, make win!
	// TODO: See if this can be implicit with makeMove
	int columnSize = gameBoard().getColumnSize();
	int rowSize = gameBoard().getRowSize();
	for(int i = 1 ; i <= columnSize ; ++i ) {
		if(Player1 == gameBoard().getOccupancy(i,rowSize)) return true;
		if(Player2 == gameBoard().getOccupancy(i,1)) return true;
	}
	// TODO: ends

	// or No piece of either side left.
	if((getPlayerPiecesCount(Player1) <= 0) || (getPlayerPiecesCount(Player2) <= 0)) {
		return true;
	}
	return false;
}

std::string BreakThrough::gameSpecialAction(const Move &gameMove){
	return "";
}

void BreakThrough::doGameSpecialAction(std::stringstream &actionStream, const Move &gameMove){
	
}

void BreakThrough::getPossibleDestinations(std::vector<std::pair<int,int> > &possibleDestinations, std::pair<int,int> &location){
	int vertical;
	if(Player1 == currentPlayer()) {
		vertical = 1;
	}
	else {
		vertical = -1;
	}
	possibleDestinations.push_back(std::pair<int,int>(location.first+1,location.second+vertical));
	possibleDestinations.push_back(std::pair<int,int>(location.first,location.second+vertical));
	possibleDestinations.push_back(std::pair<int,int>(location.first-1,location.second+vertical));
}

void BreakThrough::undoSpecialAction(std::stringstream &undoAction, std::stringstream &actionStr){
	
}

void BreakThrough::placePieces(){
	gameBoard().setBoard(columns(),rows());
	for(int i = 1 ; i <= columns() ; ++i) {
		gameBoard().placePiece(i,1,Player1);
		gameBoard().placePiece(i,2,Player1);
		gameBoard().placePiece(i,rows(),Player2);
		gameBoard().placePiece(i,rows()-1,Player2);
	}
}
	
